require '../db_help/db_help'
require 'taobao_api' 
def init_create_user
db_config1 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'tao_hidoudou'}
db_config2 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'hidoudou_staging'}
dao = DBHelp::DBHelp.out_initialize(db_config1)
dao2 = DBHelp::DBHelp.out_initialize(db_config2)
citys = dao.query("select distinct location from tao_users where location!='' and location!='诚信网商志愿者协会' and location!='延边朝鲜族'")
cts = []
citys.each do |row|
   cts << row 
end
users = dao.query("select distinct user_name from spider_item_comments where user_name not like '%**%'")
users.each do |user|
    sql = "insert into users(user_name,nick_name,city,status,flag,created_at,updated_at) values('#{user}','#{user}','#{cts[rand(cts.size)]}',0,0,'#{TaobaoAPI.timestamp}','#{TaobaoAPI.timestamp}')"
    dao2.query(sql)
end
end

def init_create_user_favorite_subject
    db_config2 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'hidoudou_staging'}
    dao2 = DBHelp::DBHelp.out_initialize(db_config2)
    users = dao2.query("select id from users")
    users.each do |row|
        sql = "insert into user_favorite_subjects(user_id,title,status,flag) values(#{row[0]},'我的收藏',0,0)"
        dao2.query(sql)
    end
    
end

def init_create_user_favorite_infos
    db_config2 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'hidoudou_staging'}
    dao2 = DBHelp::DBHelp.out_initialize(db_config2)
    3896.upto(106253) do |obj|
       items = dao2.query("select id,name,main_pic from items order by rand() limit #{rand(16)}")
       items.each do |row|
           sql = "insert into user_favorite_infos(user_favorite_subject_id,item_id,item_name,item_pic,status,created_at) values(#{obj.to_i},#{row[0]},\"#{row[1]}\",'#{row[2]}',0,'#{TaobaoAPI.timestamp}')" 
           dao2.query(sql)
       end
    end
    
end
init_create_user_favorite_infos